﻿<%@ WebHandler Language="C#" Class="EmployeeOprLogs" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class EmployeeOprLogs : AjaxHandlerBase
{

    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //添加报备相关信息
                case "getlist":
                    GetList(context);
                    break;
                case "getcombolist":
                    GetOprTypeComboList(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }


    private void GetList(HttpContext context)
    {
        Guid city = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
        Guid company = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
        Guid businessdistrict = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaid"]);
        Guid departs = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);
        Guid employee = (string.IsNullOrEmpty(context.Request["userid"])) ? Guid.Empty : new Guid(context.Request["userid"]);

        //判断权限范围/////////////////////////////////////////////
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(" 1=1 ");
        if (employee != Guid.Empty)
            sb.AppendFormat(" AND (TrackerId = '{0}') ", employee);
        else if (departs != Guid.Empty)
            sb.AppendFormat(" AND (TraceDepartId = '{0}')", departs);
        else if (businessdistrict != Guid.Empty)
        {
            Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
            string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, businessdistrict);
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
            if (!cs.Hit)
            {
                item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(businessdistrict);
                if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
            }

            if (item == null)
            {
                context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", "获取商圈或分类数据丢失"));
                return;
            }
            sb.Append(" AND (EXISTS(");
            sb.Append("Select 1 from v_Hr_DepartmentInfo M ");
            sb.AppendFormat("WHERE (System_TraceInfo.TraceDepartId = M.Departid AND M.DepartSrc like '{0}%'))) ", item.DepartSrc);
        }
        else if (company != Guid.Empty)
        {
            sb.Append(" AND (EXISTS(");
            sb.Append("Select 1 from v_Hr_DepartmentInfo M ");
            
            sb.AppendFormat("WHERE (System_TraceInfo.TraceDepartId = M.Departid AND M.CompanyId='{0}'))) ", company);
        }
        else if (city != Guid.Empty)
        {
            sb.Append(" AND (EXISTS(");
            sb.Append("Select 1 from v_Hr_DepartmentInfo M ");
            sb.AppendFormat("WHERE (System_TraceInfo.TraceDepartId = M.Departid AND M.CityId='{0}'))) ", city);
        }
        else
            sb.AppendFormat(" AND (TraceDepartId in (Select DepartId from v_Hr_DepartmentInfo where CompanyId = '{0}'))", CurrentUserInfo.CompanyId);
        
        string addtime = context.Request.Params["Addtime"];//查看时间
        string endtime = context.Request.Params["Endtime"];//查看时间
        string oprtype = context.Request.Params["OprType"];//类型

        if (!String.IsNullOrEmpty(addtime))
        {
            sb.Append(string.Format(" and  TraceTime> '{0}'", (addtime)));
        }
        if (!String.IsNullOrEmpty(endtime))
        {
            sb.Append(string.Format(" and TraceTime < '{0}'", DateTime.Parse(endtime).AddDays(1)));
        }
        if (!String.IsNullOrEmpty(oprtype))
        {
            sb.Append(" and TraceType like '%" + oprtype + "%'");
        }
        //页码
        int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
        //页显示行数
        int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数
        string strOrderField = "TraceTime desc";
        DataSet ds = Chenxin.Lib.Business.DAL.System_TraceInfo.GetList(strPageIndex, strPageSize, strOrderField, "System_TraceInfo", sb.ToString(), "*", "*", true);

        context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", ds.Tables[1].Rows[0][0], Chenxin.Lib.Utility.JsonHelp.Convert2Json(ds.Tables[0]))); 
    }

    private void GetOprTypeComboList(HttpContext context)
    {
        StringBuilder sb = new StringBuilder();

        foreach (System.Collections.DictionaryEntry de in BusinessType.BusinessTypeComboList())
        {
            sb.Append("{");
            sb.AppendFormat("txt:'{0}',val:'{1}'", de.Value, de.Key);
            sb.Append("},");
        }
        if (sb.Length > 0)
        {
            sb.Remove(sb.Length - 1, 1);
        }
        context.Response.Write(string.Format("{{success:'1',msg:'成功',data:{{items:[{0}]}}}}", sb.ToString()));
    }
}